package com.spider.ask120.webmagic.history;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jdiy.core.Rs;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.ask120.service.DiseaseKnowledgeService;
import com.spider.ask120.service.DiseaseService;
import com.spider.ask120.service.DiseaseTreatmentService;
import com.spider.ask120.webmagic.Base;
import com.spider.utils.Function;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.monitor.SpiderMonitor;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.FileCacheQueueScheduler;
import us.codecraft.webmagic.selector.Selectable;

public class Jibing extends Base implements PageProcessor{
	public static Logger log = Logger.getLogger(Jibing.class);
	static String jbUrl = "http://m.120ask.com/jibing/";
	static String urlList = "http://m.120ask.com/jibing/\\w+";
	static String shiliao = "http://m.120ask.com/jibing/\\w+/shiliao/";
	static String jieshao = "http://m.120ask.com/jibing/\\w+/jieshao/";
	
	static String bingyin = "http://m.120ask.com/jibing/\\w+/bingyin/";
	static String zhengzhuang = "http://m.120ask.com/jibing/\\w+/zhengzhuang/";
	static String jianbie = "http://m.120ask.com/jibing/\\w+/jianbie/";
	static String bingfa = "http://m.120ask.com/jibing/\\w+/bingfa/";
	static String yufang = "http://m.120ask.com/jibing/\\w+/yufang/";
	static String zhiliao = "http://m.120ask.com/jibing/\\w+/zhiliao/";
	
	//药品方案
	static String fangan = "http://m.120ask.com/yaopin/programme/[0-9]+";
	
	
		
	// 部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1);
		
	public Site getSite() { 
		return site;
	}

	//定制爬虫逻辑的核心接口
	public void process(Page page) {
		if (page.getUrl().regex(jbUrl+"$").match()) {//进入疾病搜索模块
			boolean flag = true;
			int di = 1;
			int ci = 2;
			try {
				while (flag) {
					String departments = page.getHtml().xpath("//div[@class='dis_search-div1']["+di+"]/b/text()").get();
					if (null == departments) {
						break;
					}
					//System.out.println("-----departments: " + departments+"------");
					//System.out.println(getStrName(departments));
					Rs disease = DiseaseService.getDiseaseByStrId(departments);
					Integer parent_id = null;
					if (disease.isNull()) {//判断疾病是否存在不存在则添加
						Rs dis = new Rs("disease");
						dis.set("str_id", getStrName(departments));
						dis.set("name", departments);
						dis.set("parent_id", 1);
						dis.set("level", 2);
						DiseaseService.save(dis);
						parent_id = dis.getInt("id");
					}else {
						parent_id = disease.getInt("id");
					}
					di+=2;
					int i = 1;
					while (flag) {
						String name = page.getHtml().xpath("//div[@class='dis_search-div2'][" + ci + "]/p/a["+i+"]/span/text()").get();
						if (null == name) {
							break;
						}
						String url = page.getHtml().xpath("//div[@class='dis_search-div2'][" + ci + "]/p/a["+i+"]/@href").get();
						String str_id = url.substring(url.indexOf("bing/")+5, url.length()-1);
						//System.out.println("name: "+name);
						//System.out.println("str_id: "+str_id);
						//System.out.println("url: "+url);
						i++;
						Rs dis = DiseaseService.getDiseaseByStrId(str_id);
						if (dis.isNull()) {//判断疾病是否存在
							Rs disea = new Rs("disease");
							disea.set("str_id", str_id);
							disea.set("name", name);
							disea.set("parent_id", parent_id);
							disea.set("level", 3);
							DiseaseService.save(disea);
						}
						page.addTargetRequest(url);
					}
					ci += 2;
					
				}
				List<String> list = page.getHtml().links().regex(urlList).all();
				page.addTargetRequests(list);
			} catch (Exception e) {
				log.info(e);
			}
		}else if(page.getUrl().regex(urlList).match()){
			if (page.getUrl().regex(jieshao).match()) {
				//System.out.println("jieshao:"+page.getUrl());
				//page.putField("jieshao_first_view_name", page.getHtml().xpath("//div[@class='first-view']/div[@class='lemma-head clearfix']/h1/text()"));//疾病
				//page.putField("jieshao_first_view_image", page.getHtml().xpath("//div[@class='first-view']/div[@class='card']/div[@class='clearfix']/div[@class='card-img']/img/@src"));//图片
				//page.putField("jieshao_first_view_content", page.getHtml().xpath("//div[@class='first-view']/div[@class='card']/div[@class='clearfix']/div[@class='summary']/html()"));//介绍
				//page.putField("jieshao_first_view_departments", page.getHtml().xpath("//div[@class='first-view']/div[@class='card']/p[1]/text()").all());//就诊科室
				//词条正文
//				//page.putField("jieshao_content", page.getHtml().xpath("//div[@class='content']/html()"));
				String html = page.getHtml().xpath("//div[@class='content']/html()").get();
				int i = html.indexOf("content1\"></a>");
				int m = html.indexOf("id=\"content2\"");
				String js = html.substring(i+14, m-12);
				if (i != -1 && m != -1) {
					//System.out.println("jieshao_content");
					//System.out.println(js);
//					//System.out.println(html.substring(m,html.length()));
				}
				//存入数据库
				try {
					String url = page.getUrl().get();
					String id = url.substring(url.indexOf("d_id=")+5, url.length());
					Rs dk = DiseaseKnowledgeService.getDKByDiseaseId(id);
					if (!dk.isNull()) {
						dk.set("introduce", js);
						DiseaseKnowledgeService.save(dk);
					}
				} catch (Exception e) {
					log.info(e);
				}
			}else if(page.getUrl().regex(shiliao).match()){
				//System.out.println("shiliao:"+page.getUrl());
				//疾病
				//page.putField("shiliao_jibing", page.getHtml().xpath("//div[@class='introduc_title']/b/text()"));
				JSONObject shiliao = new JSONObject();
				JSONObject yichi = new JSONObject();
				JSONArray yichiList = new JSONArray();
				//宜吃饮食必看
				//page.putField("shiliao_eat", page.getHtml().xpath("//div[@class='introduc_cont1']/span/b/text()"));
				Selectable ycbikan = page.getHtml().xpath("//div[@class='introduc_cont1']/p[@class='introduc_cont1-p']/text()");
				//page.putField("shiliao_eat_read", ycbikan);//必看
				yichi.put("bikan", ycbikan.get());
				int i = 2;
				while (true) {
					String shiliao_eat_food_list = page.getHtml().xpath("//div[@class='introduc_cont1']/table[@class='introduc_cont1-table']/tbody/tr["+i+"]/td[1]/text()").get();
					if (null == shiliao_eat_food_list) {
						break;
					}
					//System.out.println("shiliao_eat_food: "+shiliao_eat_food_list);//食物
					String reason = page.getHtml().xpath("//div[@class='introduc_cont1']/table[@class='introduc_cont1-table']/tbody/tr["+i+"]/td[2]/text()").get();
					//System.out.println("shiliao_eat_food_reason: " + reason);//理由
					String advance = page.getHtml().xpath("//div[@class='introduc_cont1']/table[@class='introduc_cont1-table']/tbody/tr["+i+"]/td[3]/text()").get();
					//System.out.println("shiliao_eat_food_advance: " + advance);//建议
					JSONObject food = new JSONObject();
					food.put("food", shiliao_eat_food_list);
					food.put("reason", reason);
					food.put("advance", advance);
					yichiList.add(food);
					i++;
				}
				//忌吃必看
				JSONObject jichi = new JSONObject();
				//page.putField("shiliao_can't_eat", page.getHtml().xpath("//div[@class='introduc_cont1 introduc_cont2']/span/b/text()"));
				Selectable jcbikan = page.getHtml().xpath("//div[@class='introduc_cont1 introduc_cont2']/p/text()");
				//page.putField("shiliao_can't_eat_read", jcbikan);
				jichi.put("bikan", jcbikan.get());
				JSONArray jichiList = new JSONArray();
				i = 2;
				while (true) {
					String shiliao_eat_food_list = page.getHtml().xpath("//div[@class='introduc_cont1 introduc_cont2']/table[@class='introduc_cont1-table']/tbody/tr["+i+"]/td[1]/text()").get();
					if (null == shiliao_eat_food_list) {
						break;
					}
					//System.out.println("shiliao_can't_eat_food: "+shiliao_eat_food_list);//食物
					String reason = page.getHtml().xpath("//div[@class='introduc_cont1 introduc_cont2']/table[@class='introduc_cont1-table']/tbody/tr["+i+"]/td[2]/text()").get();
					//System.out.println("shiliao_can't_eat_food_reason: " + reason);//理由
					String advance = page.getHtml().xpath("//div[@class='introduc_cont1 introduc_cont2']/table[@class='introduc_cont1-table']/tbody/tr["+i+"]/td[3]/text()").get();
					//System.out.println("shiliao_can't_eat_food_advance: " + advance);//建议
					JSONObject food = new JSONObject();
					food.put("food", shiliao_eat_food_list);
					food.put("reason", reason);
					food.put("advance", advance);
					jichiList.add(food);
					i++;
				}
				jichi.put("list", jichiList);
				//数据存入数据库
				try {
					String url = page.getUrl().get();
					String dt_id = url.substring(url.indexOf("d_id=")+5,url.length());
					Rs dt = DiseaseTreatmentService.getDTByDiseaseId(dt_id);
					if (!dt.isNull()) {
						shiliao.put("jichi", jichi);
						shiliao.put("yichi", yichi);
						dt.set("food_therapy", shiliao.toString());
						DiseaseTreatmentService.save(dt);
					}
				} catch (Exception e) {
					log.info(e);
				}
				
			}else if(page.getUrl().regex(bingyin).match() ||
					 page.getUrl().regex(zhengzhuang).match() ||
					 page.getUrl().regex(jianbie).match() ||
					 page.getUrl().regex(bingfa).match() || 
					 page.getUrl().regex(yufang).match() ||
					 page.getUrl().regex(zhiliao).match()){
				String url = page.getUrl().get();
				String str = url.substring(0,url.lastIndexOf("/"));
				//System.out.println("short_title:"+str);
				//page.putField("short_department", page.getHtml().xpath("//section/h3/text()"));//病种
				Selectable content = page.getHtml().xpath("//section/div/html()");
				//page.putField("short_content", content);//详情
				//page.putField("related_disease", page.getHtml().xpath("//section/div/a/text()").all());//相关疾病
				String str_id = str.substring(str.indexOf("g/")+2, str.lastIndexOf("/"));
				//System.out.println("str_id: "+str_id);
				String dep = str.substring(str.lastIndexOf("/")+1,str.length());
				//System.out.println("dep: "+dep);
				
				//存入数据库
				try {
					Rs disease_know = DiseaseKnowledgeService.getDKByStrId(str_id);
					if (!disease_know.isNull()) {//根据分类存储
						if ("bingyin".equals(dep)) {
							disease_know.set("cause", content.get());
						}
						if ("zhengzhuang".equals(dep)) {
							disease_know.set("symptom", content.get());
						}
						if ("jianbie".equals(dep)) {
							disease_know.set("identify", content.get());
						}
						if ("bingfa".equals(dep)) {
							disease_know.set("complication", content.get());
						}
						if ("yufang".equals(dep)) {
							disease_know.set("prevention", content.get());
						}
						if ("zhiliao".equals(dep)) {
							disease_know.set("treatment", content.get());
						}	
						DiseaseKnowledgeService.save(disease_know);
					}
				} catch (Exception e) {
					log.info(e);
				}
				
			}else{
				//System.out.println("gaishu:"+page.getUrl());
				
				List<String> list = page.getHtml().xpath("//ul[@class='clears s_left_sort']/li/a/@href").all();
				page.addTargetRequests(list);
				try {
					String pic_dir = fun.getProValue(JIBING_PIC_FILE_PATH);
					String url = page.getUrl().get();
					String str_id = url.substring(url.indexOf("bing/")+5,url.length()-1);//病种strid
					//page.putField("department", page.getHtml().xpath("//section/h3/text()"));//病种
					Selectable abst = page.getHtml().xpath("//section[@class='s_feritin img'][1]/div/dl/dd/text()");
					//page.putField("abstract", abst);//简介
					Selectable pic = page.getHtml().xpath("//section[@class='s_feritin img'][1]/div/dl/dt/img/@src");
					//page.putField("pic", pic);
					boolean has_drug = true;
					int i = 1;
					//用药方案
					JSONObject obj = new JSONObject();
					JSONArray array = new JSONArray();
					while(has_drug){
						List<String> drug_name = page.getHtml().xpath("//div[@class='card']/div[@class='clearfix']/div["+i+"]/img/@alt").all();
						if(0 == drug_name.size()){
							break;
						}
						//System.out.println("drug_name:"+drug_name);
						String drug_img = page.getHtml().xpath("//div[@class='card']/div[@class='clearfix']/div["+i+"]/img/@src").get();
						//System.out.println("drug_img:"+drug_img);
						JSONObject drug = new JSONObject();
						drug.put("drug_name", drug_name);
						//下载图片到本地并返回图片地址
						String pic_path = fun.downloadPic(drug_img,pic_dir+str_id+"/");
						drug.put("drug_img", pic_path);
						array.add(drug);
						i++;
					}
					obj.put("drug", array);
					//适用于
					Selectable drug_shiyong = page.getHtml().xpath("//div[@class='card']/p/text()");
					//page.putField("drug_shiyong", drug_shiyong);
					obj.put("shiyong", drug_shiyong.get());
					//方案url
					Selectable fangan_url = page.getHtml().xpath("//div[@class='card']/p/a/@href");
					//page.putField("fangan_url", fangan_url);
					//医院必看
					int j = 1;
					List<Map<String,Object>> hosp_bikan = new ArrayList<Map<String,Object>>();
					while(true){
						String fangan_hosp_bikan = page.getHtml().xpath("//section[@class='s_Tohospital t20']/ul/li["+j+"]/b/text()").get();
						if (null == fangan_hosp_bikan) {
							break;
						}
						//System.out.println("fangan_hosp_bikan: "+fangan_hosp_bikan);
						List<String> fangan_hosp_bikan_content = null;
						if (j ==2 || j == 4 || j ==5 || j ==7) {
							fangan_hosp_bikan_content = page.getHtml().xpath("//section[@class='s_Tohospital t20']/ul/li["+j+"]/span/a/text()").all();
						}else {
							fangan_hosp_bikan_content = page.getHtml().xpath("//section[@class='s_Tohospital t20']/ul/li["+j+"]/span/text()").all();
						}
						//System.out.println("fangan_hosp_bikan: "+fangan_hosp_bikan_content);
						Map<String,Object> map = new HashMap<String,Object>();
						map.put(fangan_hosp_bikan, fangan_hosp_bikan_content);
						hosp_bikan.add(map);
						j++;
					}
					//常用药
					i = 1;
					while(true){
						String fangan_changyong_name = page.getHtml().xpath("//section[@class='s_sodium']/a["+i+"]/dl/dd/h3/text()").get();
						if (null == fangan_changyong_name) {
							break;
						}
						//System.out.println("jibing_changyong_name: "+fangan_changyong_name);//药品名
						//System.out.println("jibing_changyong_image: "+page.getHtml().xpath("//section[@class='s_sodium']/a["+i+"]/dl/dt/img/@src"));//图片
						//System.out.println("jibing_changyong_indications: "+page.getHtml().xpath("//section[@class='s_sodium']/a["+i+"]/dl/dd[2]/text()"));//功能主治
						//System.out.println("jibing_changyong_company: "+page.getHtml().xpath("//section[@class='s_sodium']/a["+i+"]/dl/dd[3]/text()"));//厂商
						i++;
					}
					
					Integer d_id = null;
					Rs disease_know = DiseaseKnowledgeService.getDKByStrId(str_id);
					if (disease_know.isNull()) {
						Rs disease = DiseaseService.getDiseaseByStrId(str_id);
						if (!disease.isNull()) {
							//下载图片到本地并返回图片地址
							String pic_path = fun.downloadPic(pic.get(),pic_dir+str_id+"/");
							d_id = disease.getInt("id");
							Rs disease_k = new Rs("disease_knowledge");
							disease_k.set("str_id", str_id);
							disease_k.set("disease_id", disease.getInt("id"));
							disease_k.set("pic", pic_path);
							disease_k.set("abst", abst.get());
							DiseaseKnowledgeService.save(disease_k);
							Integer dn_id = disease_k.getInt("id");
							if (null != dn_id) {
								Rs dt = DiseaseTreatmentService.getDTByDiseaseId(disease.get("id"));
								if (dt.isNull()) {
									Rs disease_treatment = new Rs("disease_treatment");
									disease_treatment.set("disease_id", disease.getInt("id"));
									disease_treatment.set("medication_profile", obj.toString());
									disease_treatment.set("hospital_relate", JSONArray.toJSONString(hosp_bikan));
									DiseaseKnowledgeService.save(disease_treatment);
								}
							}
						}
					}else {
						Rs disease = DiseaseService.getDiseaseByStrId(str_id);
						d_id = disease.getInt("id");
					}
					page.addTargetRequest(page.getHtml().links().regex(jieshao).get()+"?d_id="+d_id);
					page.addTargetRequest(page.getHtml().links().regex(shiliao).get()+"?d_id="+d_id);
					if (null != fangan_url.get()) {
						page.addTargetRequest(fangan_url.toString()+"?d_id="+d_id);
					}
				} catch (Exception e) {
					log.info(e);
				}
				//相关疾病
				//page.putField("relation_disease", page.getHtml().xpath("//section[@class='s_disease']/div/a/text()").all());
			}
		}else if(page.getUrl().regex(fangan).match()){
			String pic_dir = fun.getProValue(JIBING_PIC_FILE_PATH);
			String url = page.getUrl().get();
			//System.out.println(url);
			//page.putField("fangan_title", page.getHtml().xpath("//section[@class='s_drugfloor clears']/h1/text()"));
			//page.putField("fangan_shiyong", page.getHtml().xpath("//div[@class='jj']/text()"));
			// 药品信息
			JSONArray durgList = new JSONArray();
			int i = 3;
			boolean flag = true;
			while(flag){
				String fangan_durg_name = page.getHtml().xpath("//div[@class='s_m1']["+i+"]/p[1]/a/text()").get();
				if (null == fangan_durg_name) {
					break;
				}
				//System.out.println("fangan_durg_name: "+fangan_durg_name);//药品名
				String image = page.getHtml().xpath("//div[@class='s_m1']["+i+"]/dt/a/img/@src").get();
				//System.out.println("fangan_durg_image: "+image);//图片
				//System.out.println("fangan_durg_link: "+page.getHtml().xpath("//div[@class='s_m1']["+i+"]/p[1]/a/@href").all());//链接
				String company = page.getHtml().xpath("//div[@class='s_m1']["+i+"]/p[2]/text()").get();
				//System.out.println("fangan_durg_company: "+company);//厂商
				String advantage = page.getHtml().xpath("//div[@class='s_m1']["+i+"]/p[3]/text()").get();
				//System.out.println("fangan_durg_advantage: "+advantage);//优点
				JSONObject durg = new JSONObject();
				durg.put("name", fangan_durg_name);
				//下载图片到本地并返回图片地址
				String pic_path = fun.downloadPic(image,pic_dir+"durg/");
				durg.put("image", pic_path);
				durg.put("company", company);
				durg.put("advantage", advantage);
				durgList.add(durg);
				i += 2;
			}
			int j = i - 1;
			String advantage = null;
			String medication_time = null;
			String medication_cycle = null;
			String important = null;
			while(flag){
				if (null == page.getHtml().xpath("//div[@class='s_m']["+j+"]/div[@class='h']/b/text()").get()) {
					break;
				}
				String dep = page.getHtml().xpath("//div[@class='s_m']["+j+"]/div[@class='h']/b/text()").get();
				//System.out.println(dep);
				String cont = page.getHtml().xpath("//div[@class='s_m']["+j+"]/div[@class='b']/html()").get();
				//System.out.println(cont);
				if ("综合优点".equals(dep)) {
					advantage = cont;
				}
				if ("用药时间".equals(dep)) {
					medication_time = cont;
				}
				if ("用药周期".equals(dep)) {
					medication_cycle = cont;
				}
				if ("注意事项".equals(dep)) {
					important= cont;
				}
				j++;
			}
			//常用药
			i = 1;
			while(flag){
				String fangan_changyong_name = page.getHtml().xpath("//section[@class='s_sodium']/a["+i+"]/dl/dd/h3/text()").get();
				if (null == fangan_changyong_name) {
					break;
				}
				//System.out.println("fangan_changyong_name: "+fangan_changyong_name);//药品名
				//System.out.println("fangan_changyong_image: "+page.getHtml().xpath("//section[@class='s_sodium']/a["+i+"]/dl/dt/img/@src"));//图片
				//System.out.println("fangan_changyong_indications: "+page.getHtml().xpath("//section[@class='s_sodium']/a["+i+"]/dl/dd[2]/text()"));//功能主治
				i++;
			}
			
			//存入数据库
			try {
				String id = url.substring(url.indexOf("id=")+3,url.length());
				Rs dt = DiseaseTreatmentService.getDTByDiseaseId(id);
				if (!dt.isNull()) {
					dt.set("medication", durgList.toJSONString());
					dt.set("advantage", advantage);
					dt.set("medication_time", medication_time);
					dt.set("medication_cycle", medication_cycle);
					dt.set("important", important);
					DiseaseKnowledgeService.save(dt);
				}
			} catch (Exception e) {
				log.info(e);
			}
		}
	}
	private String getStrName(String str){
		if ("心血管疾病".equals(str)) {
			return "xxgjb";
		}
		if ("消化系统疾病".equals(str)) {
			return "xhxtjb";
		}
		if ("呼吸系统疾病".equals(str)) {
			return "hxxtjb";
		}
		if ("妇产科疾病".equals(str)) {
			return "fkjb";
		}
		if ("儿科疾病".equals(str)) {
			return "ekjb";
		}
		if ("神经系统疾病".equals(str)) {
			return "sjxtjb";
		}
		if ("泌尿系统疾病".equals(str)) {
			return "mnxtjb";
		}
		if ("感染性疾病".equals(str)) {
			return "grxjb";
		}
		if ("风湿免疫疾病 ".equals(str)) {
			return "fsmyjb";
		}
		if ("皮肤性病科疾病".equals(str)) {
			return "pfxbkjb";
		}
		if ("耳鼻喉疾病".equals(str)) {
			return "ebhjb";
		}
		if ("骨科疾病".equals(str)) {
			return "gkjb";
		}
		if ("肿瘤科疾病".equals(str)) {
			return "zlkjb";
		}
		if ("肝胆外科疾病".equals(str)) {
			return "gdwkjb";
		}
		return null;
	}
	
	public static void runJibing(String url){
		Function fun = new Function();
		String spider_scheduler_path = fun.getProValue(JIBING_SCHEDULER_PATH);
		FileCacheQueueScheduler scheduler = new FileCacheQueueScheduler(spider_scheduler_path);
		Spider s = Spider.create(new Jibing()).addUrl(url).scheduler(scheduler);
		s.thread(1).start();
	}
	
	public static void main(String[] args) throws Exception {
		Function fun = new Function();
		String spider_scheduler_path = fun.getProValue(JIBING_SCHEDULER_PATH);
		FileCacheQueueScheduler scheduler = new FileCacheQueueScheduler(spider_scheduler_path);
		Spider s = Spider.create(new Jibing()).addUrl("http://m.120ask.com/jibing/").scheduler(scheduler);
		SpiderMonitor.instance().register(s);
		s.thread(1).start();
	}

}
